package config

import (
	"common/helper"
	"database/sql"
	"strings"
)

// 邮箱后缀黑名单
type MailSuffixItem struct {
	MailSuffix string `json:"email_suffix" db:"email_suffix" cbor:"email_suffix"`
}
type CsMailSuffixCfg struct{}

var cfgMailSuffixlst []MailSuffixItem

func (that *CsMailSuffixCfg) GetCfg() []MailSuffixItem {
	return cfgMailSuffixlst
}

/*
判断 邮箱后缀是否在黑名单中
*/
func IsBlackListMailSuffix(mail string) bool {

	address := strings.Split(mail, "@")

	if len(address) != 2 {
		return false
	}

	for _, item := range cfgMailSuffixlst {
		if strings.ToLower(address[1]) == strings.ToLower(item.MailSuffix) {
			return true
		}
	}

	return false
}

func MailSuffixSetRow(rows *sql.Rows) {
	var tVal MailSuffixItem
	var err error
	err = rows.Scan(&tVal.MailSuffix)
	if err != nil {
		helper.InfoLog("MailSuffixSetRow,scan err")
		return
	}
	cfgMailSuffixlst = append(cfgMailSuffixlst, tVal)
}

func MailSuffixRemove() {
	cfgMailSuffixlst = []MailSuffixItem{}
}
